﻿/*********************************************************
 * 开发人员：小兵
 * 创建时间：2012-10-9 9:45:15
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/
namespace Mozlite.Data
{
    using System;
    using System.Web;
    using System.Data;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Common;
    using System.Reflection;
    using System.Configuration;

    /// <summary>
    /// 辅助方法类。
    /// </summary>
    public static class DataHelper
    {
        #region helpers
        /// <summary>
        /// 将多行替换成一行字符串。
        /// </summary>
        /// <param name="str">用于替换的字符串。</param>
        /// <returns>返回转换后的字符串。</returns>
        internal static string TrimToLine(this string str)
        {
            return str.Replace(Environment.NewLine, " ").Trim();
        }
        /// <summary>
        /// 字符串格式化。
        /// </summary>
        /// <param name="format">字符串格式。</param>
        /// <param name="args">参数。</param>
        /// <returns>返回格式化后的字符串。</returns>
        internal static string FormatWith(this string format, params object[] args)
        {
            return string.Format(format, args);
        }
        #endregion

        #region sqlhelper
        //链接字符串名称。
        private const string ConnectionStringSqlHelperIdentifier = "dbtype";
        /// <summary>
        /// 创建数据库辅助接口实例对象。
        /// </summary>
        /// <param name="connectionString">链接字符串，如果为<c>null</c>，则将自动查找name为DbServer节点。</param>
        /// <returns>返回一个数据库辅助链接对象。</returns>
        /// <remarks>以后可以添加外部实例化方法，以便添加更多的数据库实例对象。</remarks>
        public static ISqlHelper CreateSqlHelper(string connectionString = null)
        {
            if(String.IsNullOrEmpty(connectionString))
            {
                var connectionStringSettings = ConfigurationManager.ConnectionStrings["DbServer"];
                if(connectionStringSettings != null)
                    connectionString = connectionStringSettings.ConnectionString;
            }
            if(String.IsNullOrEmpty(connectionString))
                throw new ArgumentNullException("connectionString");

            /* try to parse connection string */
            DbConnectionStringBuilder connectionStringBuilder = new DbConnectionStringBuilder();
            try
            {
                connectionStringBuilder.ConnectionString = connectionString;
            }
            catch(Exception ex)
            {
                throw new ArgumentException("Bad connection string.", "connectionString", ex);
            }

            string dbtype = String.Empty;
            if(connectionStringBuilder.ContainsKey(ConnectionStringSqlHelperIdentifier))
            {
                dbtype = connectionStringBuilder[ConnectionStringSqlHelperIdentifier].ToString();
                connectionStringBuilder.Remove(ConnectionStringSqlHelperIdentifier);
            }
            switch(dbtype.ToUpper())
            { 
                case "MYSQL":
                    return new MySql.MySqlHelper(connectionStringBuilder.ConnectionString);
                case "SQLCE":
                    return new SqlCE.SqlCeHelper(connectionStringBuilder.ConnectionString);
                default:
                    return new SqlServer.SqlServerHelper(connectionStringBuilder.ConnectionString);
            }
        }
        #endregion
    }
}
